package AnnotationReflect;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

@AnnoReflect(className = "AnnotationReflect.Student",methodName = "read")
// 一个放具体路径，一个放方法名字


/**
 * 1、获取类(Class)，方法(Method)，字段(Field)对象
 * 2、通过上面的对象，调用getAnnotation(注解的名字.class)，获取标注的注解对象
 * 3、通过注解的对象，获取注解里面的值
 */
public class RefletAnno {
    public static void main(String[] args) {
        Class<RefletAnno> cls = RefletAnno.class;
        AnnoReflect annotation = cls.getAnnotation(AnnoReflect.class);
        String className = annotation.className();
        String methodName = annotation.methodName();

        try {
            Class student = Class.forName(className);
            Object o = student.newInstance(); //newInstance 创建一个新对象
            Method method = student.getMethod(methodName);
            method.invoke(o);// invoke 调用方法


        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }
}
